package api

import (
	"net/http"

	"example.com/example/dao"
	"example.com/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configCustomProcessBaseEnvironmentRelRouter(router *httprouter.Router) {
	router.GET("/customprocessbaseenvironmentrel", GetAllCustomProcessBaseEnvironmentRel)
	router.POST("/customprocessbaseenvironmentrel", AddCustomProcessBaseEnvironmentRel)
	router.GET("/customprocessbaseenvironmentrel/:argEnvironmentID/:argBaseID", GetCustomProcessBaseEnvironmentRel)
	router.PUT("/customprocessbaseenvironmentrel/:argEnvironmentID/:argBaseID", UpdateCustomProcessBaseEnvironmentRel)
	router.DELETE("/customprocessbaseenvironmentrel/:argEnvironmentID/:argBaseID", DeleteCustomProcessBaseEnvironmentRel)
}

func configGinCustomProcessBaseEnvironmentRelRouter(router gin.IRoutes) {
	router.GET("/customprocessbaseenvironmentrel", ConverHttprouterToGin(GetAllCustomProcessBaseEnvironmentRel))
	router.POST("/customprocessbaseenvironmentrel", ConverHttprouterToGin(AddCustomProcessBaseEnvironmentRel))
	router.GET("/customprocessbaseenvironmentrel/:argEnvironmentID/:argBaseID", ConverHttprouterToGin(GetCustomProcessBaseEnvironmentRel))
	router.PUT("/customprocessbaseenvironmentrel/:argEnvironmentID/:argBaseID", ConverHttprouterToGin(UpdateCustomProcessBaseEnvironmentRel))
	router.DELETE("/customprocessbaseenvironmentrel/:argEnvironmentID/:argBaseID", ConverHttprouterToGin(DeleteCustomProcessBaseEnvironmentRel))
}

// GetAllCustomProcessBaseEnvironmentRel is a function to get a slice of record(s) from custom_process_base_environment_rel table in the custom_process database
// @Summary Get list of CustomProcessBaseEnvironmentRel
// @Tags CustomProcessBaseEnvironmentRel
// @Description GetAllCustomProcessBaseEnvironmentRel is a handler to get a slice of record(s) from custom_process_base_environment_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.CustomProcessBaseEnvironmentRel}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbaseenvironmentrel [get]
// http "http://localhost:8080/customprocessbaseenvironmentrel?page=0&pagesize=20" X-Api-User:user123
func GetAllCustomProcessBaseEnvironmentRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "custom_process_base_environment_rel", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllCustomProcessBaseEnvironmentRel(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetCustomProcessBaseEnvironmentRel is a function to get a single record from the custom_process_base_environment_rel table in the custom_process database
// @Summary Get record from table CustomProcessBaseEnvironmentRel by  argEnvironmentID  argBaseID
// @Tags CustomProcessBaseEnvironmentRel
// @ID argEnvironmentID
// @ID argBaseID
// @Description GetCustomProcessBaseEnvironmentRel is a function to get a single record from the custom_process_base_environment_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param  argEnvironmentID path int64 true "environment_id"
// @Param  argBaseID path int64 true "base_id"
// @Success 200 {object} model.CustomProcessBaseEnvironmentRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /customprocessbaseenvironmentrel/{argEnvironmentID}/{argBaseID} [get]
// http "http://localhost:8080/customprocessbaseenvironmentrel/1/1" X-Api-User:user123
func GetCustomProcessBaseEnvironmentRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argEnvironmentID, err := parseUint64(ps, "argEnvironmentID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_environment_rel", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetCustomProcessBaseEnvironmentRel(ctx, argEnvironmentID, argBaseID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddCustomProcessBaseEnvironmentRel add to add a single record to custom_process_base_environment_rel table in the custom_process database
// @Summary Add an record to custom_process_base_environment_rel table
// @Description add to add a single record to custom_process_base_environment_rel table in the custom_process database
// @Tags CustomProcessBaseEnvironmentRel
// @Accept  json
// @Produce  json
// @Param CustomProcessBaseEnvironmentRel body model.CustomProcessBaseEnvironmentRel true "Add CustomProcessBaseEnvironmentRel"
// @Success 200 {object} model.CustomProcessBaseEnvironmentRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbaseenvironmentrel [post]
// echo '{"environment_id": 56,"base_id": 65}' | http POST "http://localhost:8080/customprocessbaseenvironmentrel" X-Api-User:user123
func AddCustomProcessBaseEnvironmentRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	customprocessbaseenvironmentrel := &model.CustomProcessBaseEnvironmentRel{}

	if err := readJSON(r, customprocessbaseenvironmentrel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbaseenvironmentrel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbaseenvironmentrel.Prepare()

	if err := customprocessbaseenvironmentrel.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_environment_rel", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	customprocessbaseenvironmentrel, _, err = dao.AddCustomProcessBaseEnvironmentRel(ctx, customprocessbaseenvironmentrel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbaseenvironmentrel)
}

// UpdateCustomProcessBaseEnvironmentRel Update a single record from custom_process_base_environment_rel table in the custom_process database
// @Summary Update an record in table custom_process_base_environment_rel
// @Description Update a single record from custom_process_base_environment_rel table in the custom_process database
// @Tags CustomProcessBaseEnvironmentRel
// @Accept  json
// @Produce  json
// @Param  argEnvironmentID path int64 true "environment_id"// @Param  argBaseID path int64 true "base_id"
// @Param  CustomProcessBaseEnvironmentRel body model.CustomProcessBaseEnvironmentRel true "Update CustomProcessBaseEnvironmentRel record"
// @Success 200 {object} model.CustomProcessBaseEnvironmentRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbaseenvironmentrel/{argEnvironmentID}/{argBaseID} [put]
// echo '{"environment_id": 56,"base_id": 65}' | http PUT "http://localhost:8080/customprocessbaseenvironmentrel/1/1"  X-Api-User:user123
func UpdateCustomProcessBaseEnvironmentRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argEnvironmentID, err := parseUint64(ps, "argEnvironmentID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbaseenvironmentrel := &model.CustomProcessBaseEnvironmentRel{}
	if err := readJSON(r, customprocessbaseenvironmentrel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbaseenvironmentrel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbaseenvironmentrel.Prepare()

	if err := customprocessbaseenvironmentrel.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_environment_rel", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbaseenvironmentrel, _, err = dao.UpdateCustomProcessBaseEnvironmentRel(ctx,
		argEnvironmentID, argBaseID,
		customprocessbaseenvironmentrel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbaseenvironmentrel)
}

// DeleteCustomProcessBaseEnvironmentRel Delete a single record from custom_process_base_environment_rel table in the custom_process database
// @Summary Delete a record from custom_process_base_environment_rel
// @Description Delete a single record from custom_process_base_environment_rel table in the custom_process database
// @Tags CustomProcessBaseEnvironmentRel
// @Accept  json
// @Produce  json
// @Param  argEnvironmentID path int64 true "environment_id"// @Param  argBaseID path int64 true "base_id"
// @Success 204 {object} model.CustomProcessBaseEnvironmentRel
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /customprocessbaseenvironmentrel/{argEnvironmentID}/{argBaseID} [delete]
// http DELETE "http://localhost:8080/customprocessbaseenvironmentrel/1/1" X-Api-User:user123
func DeleteCustomProcessBaseEnvironmentRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argEnvironmentID, err := parseUint64(ps, "argEnvironmentID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_environment_rel", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteCustomProcessBaseEnvironmentRel(ctx, argEnvironmentID, argBaseID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
